//
//  UIImage+Draw.m
//  AzazieObjcDemo
//
//  Created by Demi on 2017/01/11.
//  Copyright © 2017 Azazie. All rights reserved.
//

#import "UIImage+Draw.h"

@implementation UIImage (Draw)

+ (void)drawColorSelectionWithColor:(UIColor *)color outerColor:(UIColor *)outerColor context:(CGContextRef)context rect:(CGRect)rect
{
    CGRect innerCircle = CGRectInset(rect, 4, 4);
    CGContextAddEllipseInRect(context, innerCircle);
    [color setFill];
    CGContextDrawPath(context, kCGPathFill);
    
    CGRect outerCircle = CGRectInset(rect, 1, 1);
    CGContextSetLineWidth(context, 2.f);
    CGContextAddEllipseInRect(context, outerCircle);
    [outerColor setStroke];
    CGContextDrawPath(context, kCGPathStroke);
}

+ (void)asyncDrawColorSelectionWithColor:(UIColor *)color outerColor:(UIColor *)outerColor size:(CGSize)size callback:(void(^)(UIImage *))callback
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        
        [UIImage drawColorSelectionWithColor:color
                                  outerColor:outerColor
                                     context:ctx
                                        rect:CGRectMake(0, 0, size.width, size.height)];
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        dispatch_async(dispatch_get_main_queue(), ^{
            callback(image);
        });
    });
}

+ (UIImage *)resizableImageWithName:(NSString *)imageName{
    UIImage *image = [UIImage imageNamed:imageName];
    // 设置不拉伸区域
    CGFloat right = image.size.width * 0.98;
    UIEdgeInsets edgeInsets = UIEdgeInsetsMake(0, 0, 0, right);
    // 设置拉伸的模式
    UIImageResizingMode mode = UIImageResizingModeStretch;
    // 拉伸图片
    UIImage *newImage = [image resizableImageWithCapInsets:edgeInsets resizingMode:mode];
    return newImage;
}

@end
